
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
/**
 *
 * @author YixinGeng This class is used to import the whole path tree from the
 * XML file and store the path tree information into a list of integer which is
 * the id of the nodes.
 */
public class XMLImporter {

    public List<GraphNode> nodeList = new ArrayList<GraphNode>();
//    public List<GraphEdge> edgeList = new ArrayList<GraphEdge>();
    private DocumentBuilderFactory docFactory;
    private DocumentBuilder docBuilder;
    private Document doc;
    public NodeList allNodes;
    public static final int DEFAULT_VALUE = -1;

    public XMLImporter() {



        /*
         * You can change the name of file here.
         */
        File XMLFile = new File("C:\\Users\\YixinGeng\\Dropbox\\XML\\statement.xml");
        try {
            docFactory = DocumentBuilderFactory.newInstance();
            docBuilder = docFactory.newDocumentBuilder();
            doc = docBuilder.parse(XMLFile);


            Element root = doc.getDocumentElement();
            allNodes = root.getChildNodes();


            for (int i = 0; i < allNodes.getLength(); i++) {

                Node node = allNodes.item(i);
                String strId = node.getAttributes().getNamedItem("id").getNodeValue().toString();
                int id = Integer.parseInt(strId);
                String content = node.getAttributes().getNamedItem("content").getNodeValue().toString();
                GraphNode myNode = new GraphNode(id, content);

                NodeList children = node.getChildNodes();
                myNode.childNum = children.getLength();

                myNode.leftNodeId = DEFAULT_VALUE;
                myNode.rightNodeId = DEFAULT_VALUE;
                myNode.childNum = 0;

                if (children.getLength() > 0) {

                    if (children.getLength() == 1) {

                        myNode.leftNodeId = Integer.parseInt(node.getChildNodes().item(0).getTextContent());
                        myNode.childNum = 1;

                        /*
                         * The GraphEdge is currently discarded. However it may
                         * be used some day when need to manipulate the edges
                         * separately.
                         *
                         */
//                            GraphEdge edge = new GraphEdge(myNode.id, myNode.leftNodeId);
//                            this.edgeList.add(edge);
                    } else {
                        myNode.childNum = 2;
                        myNode.leftNodeId = Integer.parseInt(node.getChildNodes().item(0).getTextContent());
//                            GraphEdge edgeLeft = new GraphEdge(myNode.id, myNode.leftNodeId);
//                            this.edgeList.add(edgeLeft);


                        myNode.rightNodeId = Integer.parseInt(node.getChildNodes().item(1).getTextContent());
//                            GraphEdge edgeRight = new GraphEdge(myNode.id, myNode.rightNodeId);
//                            this.edgeList.add(edgeRight);
                    }
                }
                this.nodeList.add(myNode);
            }


        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}
